#include <stdio.h>

int min(int a,int b){
    return a>=b?b:a;
}
int sq(int *h,int l,int r){
    return min(h[l],h[r])*(r-l);
}

int maxArea(int* height, int heightSize) {
    int left=0,right=heightSize-1;
    int maxA=sq(height,left,right);
    int last_left=left,last_right=right;
    while( left < right ) {
        if( height[left] <= height[right] ) {
            left++;
            if( height[left] > height[last_left] ){
                int tmp=sq(height,left,right);
                if( tmp > maxA )
                    maxA=tmp;
                last_left=left;
            }
        }else{
            right--;
            if( height[right] > height[last_right] ){
                int tmp=sq(height,left,right);
                if( tmp > maxA )
                    maxA=tmp;
                last_right=right;
            }
        }
    }
    return maxA;
}


int main(){
    int a[]={1,8,6,2,5,4,8,3,7};
    printf("%d\n",maxArea(a,sizeof(a)/sizeof(int)));
    return 0;
}
